﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SuperMarketSystem
{
    public partial class ATMForm : Form
    {
        List<TrueItem> trueItems = new List<TrueItem>();
        public ATMForm()
        {
            InitializeComponent();
			allsum.Text =  "0元";
		}

        private void btnAddItem_Click(object sender, EventArgs e)
        {
			TrueItem trueItem;
			ConfirmTrueItem confirmTrueItem = new ConfirmTrueItem();//新建一个窗口获取参数
			confirmTrueItem.ShowDialog();
			bool isFound = false;
			ListViewItem tableItemFound = null;
			trueItem = confirmTrueItem.returnTrueItem();
			if (trueItem != null)
			{
				String itemID = trueItem.ID.ToString();
				foreach (ListViewItem tableItem in itemList.Items)
				{//寻找是否有重复商品
					if (tableItem.SubItems[0].Text.Equals(itemID))
					{
						tableItemFound = tableItem;
						isFound = true;
						break;
					}
				}
				if (isFound)
				{//有重复 判断数量满足要求
					int numint = Convert.ToInt32(tableItemFound.SubItems[2].Text);
					if (numint + trueItem.num > trueItem.store - trueItem.min)
					{//不满足要求 阻止
						MessageBox.Show("阻止行为", "试图添加到不可售卖的数量");
					}
					else
					{//满足要求 合并
						int wantToBuy = numint + trueItem.num;
						double priceToBuy = trueItems[(itemList.Items.IndexOf(tableItemFound))].realSumPrice + trueItem.realSumPrice;
						tableItemFound.SubItems[2].Text = wantToBuy.ToString();
						tableItemFound.SubItems[3].Text = priceToBuy.ToString();
						trueItems[(itemList.Items.IndexOf(tableItemFound))].num = wantToBuy;//修改对应商品数量
						trueItems[(itemList.Items.IndexOf(tableItemFound))].realSumPrice = priceToBuy;//修改单项目总价
					}
				}
				else
				{//没找到 新建一个项目
					tableItemFound = new ListViewItem();
					trueItems.Add(trueItem);//添加商品
					tableItemFound.Text = trueItem.ID.ToString();
					tableItemFound.SubItems.Add(trueItem.name);
					tableItemFound.SubItems.Add(trueItem.num.ToString());
					tableItemFound.SubItems.Add(trueItem.realSumPrice.ToString());
					itemList.Items.Add(tableItemFound);
				}
				Double allDouble = 0.0;
				foreach (TrueItem trueItem2 in trueItems)
				{//计算购物车总价
					allDouble += trueItem2.realSumPrice;
				}
				allsum.Text = allDouble.ToString() + "元";
			}
		}

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
			if (itemList.SelectedItems.Count > 0)
			{
				trueItems.RemoveAt(itemList.Items.IndexOf(itemList.SelectedItems[0]));
				itemList.Items.Remove(itemList.SelectedItems[0]);
				Double allDouble = 0.0;
				foreach (TrueItem trueItem2 in trueItems)
				{
					allDouble += trueItem2.realSumPrice;
				}
				allsum.Text = Convert.ToString(allDouble) + "元";
			}
            else
            {
				MessageBox.Show("警告", "至少选中一项");
            }
        }

        private void btnCheckOut_Click(object sender, EventArgs e)
        {
			foreach (TrueItem trueItem in trueItems)
			{
				SqlConnection connection1 = MySqlServerUtils.getMySqlServerConnection();
				SqlCommand preparedStatementPre = null, preparedStatement = null, preparedStatement2 = null, preparedStatement3 = null;
				bool isSelect = false;
				int isUpdate = 0;
				try
				{
					SqlConnection connection2 = MySqlServerUtils.getMySqlServerConnection();
					SqlConnection connection3 = MySqlServerUtils.getMySqlServerConnection();
					SqlConnection connection4 = MySqlServerUtils.getMySqlServerConnection();
					String sql2 = String.Format("select * from stores where itemID={0}", trueItem.ID);
					String sql3 = String.Format("update stores set itemstore=itemstore-{0} where itemID={1}", trueItem.num, trueItem.ID);
					String sql4 = "select max(outID) as max_outID,max(outroot) as max_outroot from outlist";
					int remain = -1;
					int remin = -2;
					int trueItem_OutID;
					int trueItem_OutRoot;
					preparedStatementPre = new SqlCommand(sql4, connection4);
					SqlDataReader resultSetPre = preparedStatementPre.ExecuteReader();
					if (resultSetPre.Read())
					{
						trueItem_OutID = Convert.ToInt32(resultSetPre["max_outID"]) + 1;
						trueItem_OutRoot = Convert.ToInt32(resultSetPre["max_outroot"]) + 1;
					}
					else
					{
						trueItem_OutID = 1;
						trueItem_OutRoot = 1;
					}
					String sql1 = String.Format("insert into outlist(outID,itemID,outprice,outnum,outsum,outroot,outdate)" +
				   " values({0},{1},{2},{3},{4},{5},'{6}')", trueItem_OutID, trueItem.ID, trueItem.priceafterdiscount, trueItem.num, trueItem.realSumPrice, trueItem_OutRoot, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
					preparedStatement = new SqlCommand(sql1, connection1);
					preparedStatement2 = new SqlCommand(sql2, connection2);
					SqlDataReader set = preparedStatement2.ExecuteReader();
					isSelect = set.Read();
					if (isSelect)
					{
						remain = Convert.ToInt32(set["itemstore"]);
						remin = Convert.ToInt32(set["itemmin"]);
					}
					preparedStatement3 = new SqlCommand(sql3, connection3);
					if (remain > 0 && remin > 0 && remain - trueItem.num >= remin)
					{
						isUpdate = preparedStatement3.ExecuteNonQuery();
					}
					if (isSelect != false && isUpdate != 0)
					{
						preparedStatement.ExecuteNonQuery();
					}
					else
					{
						MessageBox.Show("操作回滚", "对应书目库存不够或者不存在书目，无法更新");
					}
					MySqlServerUtils.closeSqlServerConnection(connection1);
					MySqlServerUtils.closeSqlServerConnection(connection2);
					MySqlServerUtils.closeSqlServerConnection(connection3);
					MySqlServerUtils.closeSqlServerConnection(connection4);
				}
				catch (Exception e2)
				{
					// TODO Auto-generated catch block
					
				}
			}
			itemList.Items.Clear();
			trueItems.Clear();
			allsum.Text="0元";
		}
    }
}
